package com.groupbuying.cheetah.settle.settleRefundParamsInit.controller;

import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;
import com.groupbuying.cheetah.settle.settleRefundParamsInit.domian.SettleUshopFlowStoreDO;
import com.groupbuying.cheetah.settle.settleRefundParamsInit.service.SettleUshopFlowStoreService;
import com.groupbuying.cheetah.utils.JsonUtils;
import com.groupbuying.cheetah.utils.apiresult.ApiResult;
import com.groupbuying.cheetah.utils.apiresult.ApiResultCode;
import com.groupbuying.cheetah.utils.apiresult.DateUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优店商户资金流水表
 * 
 * @author cuich
 * @email cuichanghao@linku.com.cn
 * @date 2019-06-18 02:37:20
 */
 
@RestController
@RequestMapping("/settleproducer/settleUshopFlowStore")
public class SettleUshopFlowStoreController {
	private static final Logger logger = LoggerFactory.getLogger(SettleUshopFlowStoreController.class);

	@Autowired
	private SettleUshopFlowStoreService settleUshopFlowStoreService;
	
	@GetMapping()
	@RequiresPermissions("settleproducer:settleUshopFlowStore:settleUshopFlowStore")
	String SettleUshopFlowStore(){
	    return "settleproducer/settleUshopFlowStore/settleUshopFlowStore";
	}

	/**
	 * 通过收银台ID获取商户当前余额
	 */
	@GetMapping("/getStoreBalanceByCheckstandId")
	@RequiresPermissions("settleproducer:settleUshopFlowStore:settleUshopFlowStore")
	public BigDecimal getStoreBalanceByCheckstandId(@RequestParam String checkstandId){
        BigDecimal sumAmount = settleUshopFlowStoreService.getStoreBalanceByCheckstandId(checkstandId);
        return sumAmount==null?new BigDecimal("0.00"):sumAmount;
	}

	/**
	 * 通过收银台ID获取商户当前余额
	 */
	@GetMapping("/getStoreTodayIncomeByCheckstandId")
	public BigDecimal getStoreTodayIncomeByCheckstandId(@RequestParam String checkstandId){
		Map<String,Object> params = new HashMap<>();
		params.put("checkstandId",checkstandId);
		params.put("flowType","0");
		params.put("isDelete","1");
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YMD);
		String gmtCreate = sdf.format(new Date());
		params.put("gmtCreate",gmtCreate);
		List<Map<String,Object>> sumAmountList = settleUshopFlowStoreService.getStoreFlowListByCheckstandId(params);
		logger.error("getStoreTodayIncomeByCheckstandId sumAmountList:"+ JsonUtils.objectToJson(sumAmountList));
		BigDecimal sumAmount ;
		if(sumAmountList.isEmpty()){
			sumAmount = new BigDecimal("0.00");
		}else{
			Map<String,Object> result = sumAmountList.get(0);
			sumAmount = (BigDecimal) result.get("amount");
		}
		return sumAmount;
	}

	/**
	 * 通过收银台ID获取商户流水列表
	 */
	@GetMapping("/getStoreFlowListByCheckstandId")
	@RequiresPermissions("settleproducer:settleUshopFlowStore:settleUshopFlowStore")
	public ApiResult getStoreFlowListByCheckstandId(@RequestParam Map<String,Object> params){
		logger.error("SettleUshopFlowStoreController-->getStoreFlowListByCheckstandId start params:::"+ JsonUtils.objectToJson(params));
		//校验请求参数
		if(MapUtils.isEmpty(params)){
			return ApiResult.failure(ApiResultCode.PARAMNULL);
		}
		Boolean paramsFlag = checkRefundParams(params);
		if(!paramsFlag){
			return ApiResult.failure(ApiResultCode.PARAMERROR);
		}
		Query query = new Query(params);
        List<Map<String,Object>> sumAmountList = settleUshopFlowStoreService.getStoreFlowListByCheckstandId(query);
		int total = settleUshopFlowStoreService.countByTime(query);
		PageUtils pageUtils = new PageUtils(sumAmountList, total);
        return ApiResult.success(pageUtils);
	}

	@GetMapping("/list")
	@RequiresPermissions("settleproducer:settleUshopFlowStore:settleUshopFlowStore")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SettleUshopFlowStoreDO> settleUshopFlowStoreList = settleUshopFlowStoreService.list(query);
		int total = settleUshopFlowStoreService.count(query);
		PageUtils pageUtils = new PageUtils(settleUshopFlowStoreList, total);
		return pageUtils;
	}
	

	private boolean checkRefundParams(Map<String,Object> params){
		String gmtCreateStart = (String) params.get("gmtCreateStart");
		String gmtCreateEnd = (String) params.get("gmtCreateEnd");
		String checkstandId = (String) params.get("checkstandId");
		String isDelete = (String) params.get("isDelete");
		String flowType = (String) params.get("flowType");
		if(StringUtils.isNotEmpty(gmtCreateStart) && StringUtils.isEmpty(gmtCreateEnd)){
			return false;
		}
		if(StringUtils.isEmpty(checkstandId)){
			return false;
		}
		if(StringUtils.isEmpty(isDelete)){
			return false;
		}
		if(StringUtils.isEmpty(flowType)){
			return false;
		}
		return true;
	}
}
